package com.example.textviewactivity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.textviewactivity.Receiver.MyReceiver;
import com.example.textviewactivity.Service.MyService;
import com.example.textviewactivity.pojo.User;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class LoginActivity extends AppCompatActivity {
    EditText ed_name, ed_pwd;
    TextView logon;
    CheckBox save;
    User user1 = new User();
    User user;
    MyReceiver myReceiver;
    SharedPreferences sp;
    private static final String TAG = "LoginActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        logon = findViewById(R.id.logon);
        ed_name = findViewById(R.id.ed_name);
        ed_pwd = findViewById(R.id.ed_pwd);
        save = findViewById(R.id.save);
        sp = getSharedPreferences("user", Context.MODE_PRIVATE);
        savePwd();
        logon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, LogonActivity.class);
                startActivityForResult(intent, 200);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 200) {
            user = (User) data.getSerializableExtra("user");
            ed_name.setText(user.getName() + "");
            ed_pwd.setText(user.getPassword() + "");
            user1.setName(user.getName());
            user1.setPhone(user.getPhone());
            user1.setPassword(user.getPassword());
            user1.setHobby(user.getHobby());
            user1.setSex(user.getSex());
            Log.d(TAG, "onActivityResult: " + user);
        }
    }

    public void login(View view) {
        String name = ed_name.getText().toString();
        String password = ed_pwd.getText().toString();
        SharedPreferences.Editor edit = sp.edit();
        if (save.isChecked()) {
            myReceiver = new MyReceiver();
            IntentFilter filter = new IntentFilter();
            filter.addAction("com.example.textviewactivity.Receiver.MyReceiver");
            registerReceiver(myReceiver, filter);
            Intent in = new Intent(LoginActivity.this, MyService.class);
            in.putExtra("user", user);
            in.putExtra("name", name);
            in.putExtra("password", password);
            startService(in);
            edit.putString("name", name);
            edit.putBoolean("flag", true);
            edit.commit();
            try {
                FileOutputStream outputStream = openFileOutput("user.txt", Context.MODE_PRIVATE);
                outputStream.write(password.getBytes());
                outputStream.flush();
                outputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            edit.putBoolean("flag", false);
            edit.commit();
            Toast.makeText(LoginActivity.this, "勾选记住密码才能登陆", Toast.LENGTH_SHORT).show();
        }
    }

    public void savePwd() {
        boolean flag = sp.getBoolean("flag", false);
        save.setChecked(flag);
        if (flag) {
            String name = sp.getString("name", null);
            ed_name.setText(name);
            try {
                FileInputStream inputStream = openFileInput("user.txt");
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader stream = new BufferedReader(inputStreamReader);
                String passwrod = stream.readLine();
                ed_pwd.setText(passwrod);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
